package com.team.arms.yuding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.arms.bingyiInfo.entity.BingyiInfo;
import com.team.arms.common.Result;
import com.team.arms.common.ry.utils.StringUtils;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.public_data.domain.TroopInfo;
import com.team.arms.utils.ExcelUtil;
import com.team.arms.yuding.domain.DoubleQualified;
import com.team.arms.yuding.domain.dto.ReservedDto;
import com.team.arms.yuding.domain.vo.ReservedVo;
import com.team.arms.yuding.mapper.DoubleQualifiedMapper;
import com.team.arms.yuding.mapper.ReservedMapper;
import com.team.arms.yuding.service.ReservedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yjh
 * @create 2022-05-07-20:52
 * @description:
 */
@Service
public class ReservedServiceImpl extends ServiceImpl<ReservedMapper, ReservedVo>
            implements ReservedService {

    @Autowired
    private ReservedMapper reservedMapper;

    @Autowired
    private ConvertUtil convertUtil;

    @Override
    public List<ReservedVo> selectAll() {
        return reservedMapper.selectAll();
    }

//    @Override
//    public List<ReservedVo> selectByName(String name) {
//        return reservedMapper.selectByName(name);
//    }



    /**
     * 按条件查询已预订信息
     * @param reservedDto
     * @return
     */
    @Override
    public List<ReservedVo> selectReserved(ReservedDto reservedDto) {
        return reservedMapper.selectReserved(reservedDto);
    }

    @Override
    public Result importExcel(MultipartFile file) {
        //获取文件名
        String filename = file.getOriginalFilename();
        String pattern = filename.substring(filename.lastIndexOf(".") + 1);
        List<List<String>> listContent = new ArrayList<>();
        if (file != null) {
            if (!ExcelUtil.isExcel(file)) {
                return Result.error("导入失败，该文件不是excel文件");
            }
            listContent = ExcelUtil.readExcelContents(file, pattern);
            if (listContent.isEmpty()) {
                return Result.error("表格为空");
            }
            for (int i = 0; i < listContent.size(); i++) {

                DoubleQualified doubleQualified = new DoubleQualified();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//设置时间格式
                doubleQualified.setName(listContent.get(i).get(1));
                doubleQualified.setSex(listContent.get(i).get(2));
                doubleQualified.setYear(listContent.get(i).get(3));
                doubleQualified.setBatch(listContent.get(i).get(4));
                doubleQualified.setLoginNumber(listContent.get(i).get(5));
                doubleQualified.setAge(listContent.get(i).get(6));
                doubleQualified.setNation(listContent.get(i).get(7));
                doubleQualified.setHouseholdType(listContent.get(i).get(8));
                doubleQualified.setCultureralProgram(listContent.get(i).get(9));
                try {
                    doubleQualified.setRegistrationTime(sdf.parse(listContent.get(i).get(10)));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                doubleQualified.setIsOrderSoldiers(listContent.get(i).get(11));
                doubleQualified.setJoinApproveId(listContent.get(i).get(12));
                doubleQualified.setJoinInformId(listContent.get(i).get(13));

                reservedMapper.insertAll(doubleQualified);
            }
            return Result.success(listContent);
        }
        return Result.error("未选择文件");
    }
}
